import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 单位显示测试
 * 验证"台"和"辆"只显示数字，不显示单位文字
 */
public class UnitDisplayTest {
    
    public static void main(String[] args) {
        System.out.println("=== 单位显示测试 ===");
        
        // 测试重量单位（保持原样）
        testWeightUnits();
        
        // 测试计数单位（台、辆只显示数字）
        testCountUnits();
        
        // 测试完整示例
        testCompleteExample();
    }
    
    /**
     * 测试重量单位显示
     */
    private static void testWeightUnits() {
        System.out.println("\n--- 重量单位显示测试（保持原样）---");
        
        testFormat("672.669", "kg", "672.67吨");
        testFormat("0.686", "kg", "0.69吨");
        testFormat("0.037", "kg", "0.04吨");
        testFormat("0.000119", "g", "0.00吨");
        testFormat("1200.5", "kg", "1.20千吨");
        testFormat("2500", "kg", "2.50千吨");
    }
    
    /**
     * 测试计数单位显示
     */
    private static void testCountUnits() {
        System.out.println("\n--- 计数单位显示测试 ---");
        
        // 台和辆：只显示数字
        System.out.println("台和辆（只显示数字）：");
        testFormat("8.7", "台", "9");
        testFormat("3.2", "台", "3");
        testFormat("10.9", "台", "11");
        testFormat("5.3", "辆", "5");
        testFormat("12.8", "辆", "13");
        testFormat("1.1", "辆", "1");
        
        // 个和件：显示数字+单位
        System.out.println("\n个和件（显示数字+单位）：");
        testFormat("150.4", "个", "150个");
        testFormat("85.7", "件", "86件");
        testFormat("200.2", "个", "200个");
        testFormat("99.9", "件", "100件");
    }
    
    /**
     * 测试完整示例
     */
    private static void testCompleteExample() {
        System.out.println("\n--- 完整示例测试 ---");
        
        System.out.println("chartTypeList 示例：");
        System.out.println("[");
        
        // 重量类型
        System.out.println("  {\"name\": \"纸品类\", \"value\": \"" + 
            formatValueWithUnit(new BigDecimal("672.669"), "kg") + "\", \"unit\": \"吨\"},");
        System.out.println("  {\"name\": \"旧衣类\", \"value\": \"" + 
            formatValueWithUnit(new BigDecimal("0.686"), "kg") + "\", \"unit\": \"吨\"},");
        System.out.println("  {\"name\": \"大型废料\", \"value\": \"" + 
            formatValueWithUnit(new BigDecimal("1200.5"), "kg") + "\", \"unit\": \"千吨\"},");
        
        // 计数类型 - 台和辆（只显示数字）
        System.out.println("  {\"name\": \"家电\", \"value\": \"" + 
            formatValueWithUnit(new BigDecimal("8.7"), "台") + "\", \"unit\": \"台\"},");
        System.out.println("  {\"name\": \"车辆类\", \"value\": \"" + 
            formatValueWithUnit(new BigDecimal("5.3"), "辆") + "\", \"unit\": \"辆\"},");
        
        // 计数类型 - 个和件（显示数字+单位）
        System.out.println("  {\"name\": \"锂电池类\", \"value\": \"" + 
            formatValueWithUnit(new BigDecimal("150.4"), "个") + "\", \"unit\": \"个\"},");
        System.out.println("  {\"name\": \"服装\", \"value\": \"" + 
            formatValueWithUnit(new BigDecimal("85.7"), "件") + "\", \"unit\": \"件\"}");
        
        System.out.println("]");
    }
    
    /**
     * 测试单个格式化
     */
    private static void testFormat(String valueStr, String unit, String expected) {
        BigDecimal value = convertToStandardUnit(new BigDecimal(valueStr), unit);
        String result = formatValueWithUnit(value, unit);
        String status = result.equals(expected) ? "✓" : "✗";
        System.out.println(status + " " + valueStr + unit + " -> \"" + result + "\" (期望: \"" + expected + "\")");
    }
    
    /**
     * 转换为标准单位
     */
    private static BigDecimal convertToStandardUnit(BigDecimal value, String unit) {
        String unitLower = unit.toLowerCase().trim();
        switch (unitLower) {
            case "kg":
            case "千克":
                return value.divide(new BigDecimal("1000"), 6, RoundingMode.HALF_UP);
            case "g":
            case "克":
                return value.divide(new BigDecimal("1000000"), 6, RoundingMode.HALF_UP);
            default:
                return value;
        }
    }
    
    /**
     * 格式化值并添加单位
     */
    private static String formatValueWithUnit(BigDecimal value, String originalUnit) {
        if (value == null) {
            value = BigDecimal.ZERO;
        }
        
        String unitLower = originalUnit.toLowerCase().trim();
        switch (unitLower) {
            case "吨":
            case "t":
            case "kg":
            case "千克":
            case "g":
            case "克":
                // 重量类型：根据大小决定显示吨还是千吨
                BigDecimal formattedWeight = value.setScale(2, RoundingMode.HALF_UP);
                if (formattedWeight.compareTo(new BigDecimal("1000")) >= 0) {
                    BigDecimal weightInKiloTons = formattedWeight.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP);
                    return weightInKiloTons.toString() + "千吨";
                } else {
                    return formattedWeight.toString() + "吨";
                }
            case "台":
            case "辆":
                // 台和辆：只显示数字，不显示单位
                BigDecimal countValue = value.setScale(0, RoundingMode.HALF_UP);
                return countValue.toString();
            case "个":
            case "件":
                // 个和件：保留整数并添加单位
                BigDecimal countValue2 = value.setScale(0, RoundingMode.HALF_UP);
                return countValue2.toString() + originalUnit;
            default:
                // 其他类型：保留2位小数
                BigDecimal defaultValue = value.setScale(2, RoundingMode.HALF_UP);
                return defaultValue.toString();
        }
    }
}
